package site.teamo.biu.net.server.interceptor;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import site.teamo.biu.net.common.constant.HttpHeader;
import site.teamo.biu.net.common.web.constant.ResponseCode;
import site.teamo.biu.net.server.service.ClientManagementService;

/**
 * @author haocongshun
 * @date 2023/03/29 13:43:29
 */
@Component
public class ReportInterceptor implements HandlerInterceptor {

    @Resource(name = "clientManagementServiceImpl")
    private ClientManagementService clientManagementService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader(HttpHeader.ACCESS_TOKEN);
        if (clientManagementService.isExist(token).booleanValue) {
            return true;
        }
        request.setAttribute("responseCode", ResponseCode.AUTHENTICATION_FAILED_ERROR);
        request.getRequestDispatcher("/exception").forward(request, response);
        return false;
    }
}
